package com.fxu.gen;

import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;
import com.fxu.framework.core.enums.BaseEnum;
import com.fxu.tpl.entity.Column;

import java.util.List;

/**
 * 生成代码工具类
 * @author fangxu
 */
public class AutoUtil {
	/**
	 * 首字母小写写，且转化成驼峰命名 user_role : userRole
	 * @param name 表名或者字段名
	 * @return 返回首字母是小写的驼峰命名字符串
	 */
	public static String firstLowerBean(String name) {
		return StrUtil.lowerFirst(getBean(name));
	}

	/**
	 * 下划线转换成驼峰 user_role : UserRole
	 * @param name 表名或者字段名
	 */
	public static String getBean(String name) {
		if (name == null || name.length() <= 0)
			return null;
		final int len = name.length();
		StringBuilder buffer = new StringBuilder(len + 3);

		if (Character.isLowerCase(name.charAt(0))) {
			buffer.append(Character.toUpperCase(name.charAt(0)));
		} else {
			buffer.append(name.charAt(0));
		}
		for (int i = 1; i < len; i++) {
			if (name.charAt(i) == '_') {
				buffer.append(Character.toUpperCase(name.charAt(i+1)));
				i = i + 1;
			} else {
				buffer.append(name.charAt(i));
			}
		}
		return buffer.toString();
	}

	/**
	 * 字段名是否包含_数字 user_10
	 * @param name 表名或者字段名
	 */
	public static boolean hasSpecialNumber(String name) {
		if (name == null || name.length() <= 0)
			return false;
		for (int i = 1, len = name.length(); i < len; i++) {
			if (name.charAt(i) == '_' && (i + 1 >= len || (i + 1 < len && CharUtil.isNumber(name.charAt(i + 1))))) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 去掉最后的逗号[和(换行或者空格)]
	 * @param buffer 需要处理内容
	 * @return 处理后内容
	 */
	public static StringBuffer moveLastDot(final StringBuffer buffer) {
		if (buffer.length() <= 0) return buffer;
		if (buffer.charAt(buffer.length() - 1) == ' ' && buffer.charAt(buffer.length() - 2) == ',') {
			buffer.setLength(buffer.length() - 2);
		} else if (buffer.charAt(buffer.length() - 1) == '\n' && buffer.charAt(buffer.length() - 2) == ',') {
			buffer.setLength(buffer.length() - 2);
		} else if (buffer.charAt(buffer.length() - 1) == ',') {
			buffer.setLength(buffer.length() - 1);
		}
		return buffer;
	}

	/**
	 * 获得MySQL数据库中的类型
	 * @param column 字段
	 * @return 返回SQL类型
	 */
	public static String getSqlType(Column column) {
		Integer type = column.getType();
		if (type == null || type < 0) return "";
		switch (type) {
		case 1:
			if (column.getLength() < 256) {
				return "char";
			}
			return "varchar";
		case 2:
			//TODO (1)
			return "tinyint";
		case 3:
			return "int";
		case 4:
			return "bigint";
		case 5:
			return "float";
		case 6:
			return "double";
		case 7:
			return "decimal";
		case 8:
			return "datetime";
		case 11:
			return "date";
		case 9:
			return "text";
		case 10:
			return "mediumtext";
		default:
			return "";
		}
	}

	//1:String,2:Boolean,3:Integer,4:Long,5:Float,6:Double,7:Decimal,8:Date,9:Text,10:Blob
	public static int getTypeBySql(String sqlType, int length) {
		if (StrUtil.isEmpty(sqlType)) {
			return 0;
		}
		sqlType = sqlType.toLowerCase();
		switch (sqlType) {
		case "char":
		case "varchar":
			return 1;
		case "tinyint":
			if (length == 1)
				return 2;
			return 3;
		case "int":
			return 3;
		case "bigint":
			return 4;
		case "float":
			return 5;
		case "double":
			return 6;
		case "decimal":
			return 7;
		case "date":
			return 11;
		case "datetime":
		case "timestamp":
			return 8;
		case "text":
		case "tinyblob":
			return 9;
		case "longtext":
		case "mediumtext":
			return 10;
		default:
			return 0;
		}
	}

	//1:文本框,2:选择器,3:日期,4:日期时间,5:数字,6:单选,7:多选,8:多行文本,9:富媒体,10:图片,11:文件,12:密文
	public static int getFormTypeBySql(String name, String sqlType, int length) {
		if (StrUtil.isEmpty(name) || StrUtil.isEmpty(sqlType)) {
			return 0;
		}
		sqlType = sqlType.toLowerCase();
		switch (sqlType) {
		case "char":
		case "varchar":
			if (name.endsWith("_id")) {
				return 2;
			}
			if (length > 512)
				return 9;
			if (length > 255)
				return 8;
			return 1;
		case "tinyint":
			return 6;
		case "int":
		case "bigint":
		case "float":
		case "double":
		case "decimal":
			return 5;
		case "date":
			return 3;
		case "datetime":
			return 4;
		case "text":
		case "mediumtext":
			return 8;
		default:
			return 0;
		}
	}

	/**
	 * 根据字段名字判断字段是否存在
	 * @param columns 字段对象列表
	 * @param columnName 字段名
	 * @return 存在: true, 不存在: false
	 */
	public static boolean hasColumn(List<Column> columns, String columnName) {
		return BaseEnum.hasAny(columns, column -> column.getName().equals(columnName));
	}
	
	/**
	 * 加入import列表
	 * @param imports 已存在的import列表
	 * @param newImport 新增加的import
	 */
	public static void toImports(List<String> imports, String newImport) {
		if (imports != null && newImport != null && !imports.contains(newImport)) {
			imports.add(newImport);
		}
	}

	/**
	 * 加入import列表
	 * @param imports 已存在的import列表
	 * @param newImport 新增加的import
	 */
	public static void toImports(List<String> imports, Class<?> newImport) {
		if (imports != null && newImport != null && !imports.contains(newImport.getName())) {
			imports.add(newImport.getName());
		}
	}
	
	public static void main(String[] args) {
		
	}
}
